<?php

    class InvoiceUtil
    {

        public static function getBillAmountForInvoiceAttachedToSaleOrders(Invoice $salesOrder)
        {
            //look for all invoice
            //look for all creditmemo
            $total = 0;
            $invoiceList = $salesOrder->findInvoiceModelListFromSalesOrder();
            foreach ($invoiceList as $invoice)
            {
                $total+=$invoice->amountcharge;
                foreach ($invoice->creditMemoList as $creditMemo)
                {
                    $total+=$creditMemo->amountcharge;
                }
            }
            return $total;
        }

        /**
         * Check is invoice using tax nex
         * @param string $version Invoice version
         * @param string $shipAddress ShipCSZ
         * @return boolean
         */
        public static function doesInvoiceUseTaxNex($version, $shipAddress)
        {
            if ($version != 1)
            {
                $stateData = explode(' ', $shipAddress);
                $stateName = $stateData[count($stateData) - 2];
                $stateZip = $stateData[count($stateData) - 1];
                $taxCodeNex = TaxcodeNex::model()->find("state=:state", array(
                    'state' => $stateName
                        ));

                if (isset($taxCodeNex) && $taxCodeNex->active)
                {
                    return true;
                }
            }
            return false;
        }

    }

?>
